import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

const state = {
    num:0
}

const getters = {
    evenOrOdd(){
        return state.num % 2 == 1 ? "奇数":"偶数"
    }
}

const mutations = {
    // 第一个参数：state  参数2：commit触发mutations当中的add传递过来的参数
   /*  add(state,str){
        console.log(state);
        console.log(str);
        state.num +=1;
    } */
    add(state,{name1,name2}){
        console.log(state);
        console.log(name1,name2);
        state.num +=1;
    },
    jian(state,{count}){
        state.num -= count*1;
    }
}

const actions = {
    oddAdd({state,commit},{name1,name2}){
        console.log(name1,name2);
        if(state.num % 2 == 1){
            commit("add",{name1,name2});
        }
    },
    evenJian({state,commit},{count}){
        if(state.num % 2 == 0){
            commit("jian",{count})
        }
    }
}

//暴露
export default new Vuex.Store({
    state,
    mutations,
    getters,
    actions
})